import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import javax.xml.bind.Element;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
        //test01();
        //test02();
        //报错
        //test03();
        //test04();
        //test05();
        //test06();
        //test07();
        //test08();
        //test09();
        //page01();
        //page02();
        //page03();
        //针对alter
        //page04();
        //page05();
        //test10();
        //test11();
        test12();
    }

    private static void test12() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("E:\\selenium\\20230805.png"));
    }

    private static void test11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        //通过getWindowHandles获取所有窗口句柄
        //通过getWindowHandle获取get打开的窗口句柄
        System.out.println(webDriver.getWindowHandle());
        Set<String> handles =  webDriver.getWindowHandles();
        String target_handle = "";
        for (String handle : handles) {
            target_handle = handle;
        }
        webDriver.switchTo().window(target_handle);
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
    }

    private static void test10() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        //webDriver.quit();
        webDriver.close();
    }

    private static void page05() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/selenium/src/main/page/test07.html?_ijt=pt9e6e6fsi9me88r9p5hemtgqk&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("body > div > div > input[type=file]")).
                sendKeys("D:\\专升本");
    }

    private static void page04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/selenium/src/main/page/test06.html?_ijt=jctl0ss5qljqma8m2cpo5eqibs&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("body > input[type=button]")).click();
        sleep(3000);
        //alter弹窗取消
        webDriver.switchTo().alert().dismiss();
        sleep(3000);
        //点击按钮
        webDriver.findElement(By.cssSelector("body > input[type=button]")).click();
        //在alter弹窗输入
        webDriver.switchTo().alert().sendKeys("hyj");
        //弹窗确认
        sleep(3000);
        webDriver.switchTo().alert().accept();

    }

    private static void page03() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/selenium/src/main/page/test04.html?_ijt=r4rd8t6gu09o500699l1dbj4nq&_ij_reload=RELOAD_ON_SAVE");
        WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(webElement);
        //select.selectByIndex(3);
        select.selectByValue("8.34");

    }

    private static void page02() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/selenium/src/main/page/test02.html?_ijt=vh8m42hqjv41a97prlr07e3dnb&_ij_reload=RELOAD_ON_SAVE");
        webDriver.switchTo().frame("f1");
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

    private static void page01() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/selenium/src/main/page/test01.html?_ijt=rg9tvpis5ntap31m3a52a96km5&_ij_reload=RELOAD_ON_SAVE");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.DAYS);
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
        for (int i = 0; i < webElements.size(); i++) {
            if (webElements.get(i).getAttribute("type").equals("checkbox")) {
                webElements.get(i).click();
            }else {
                ;
            }
        }
    }

    private static void test09() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        WebElement webElement = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
        Actions actions = new Actions(webDriver);
        sleep(3000);
        actions.moveToElement(webElement).contextClick(webElement).perform();

    }

    private static void test08() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"a");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"x");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"v");

    }

    private static void test07() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        webDriver.navigate().back();
        sleep(3000);
        webDriver.navigate().refresh();
        sleep(3000);
        webDriver.navigate().forward();
        sleep(3000);
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        webDriver.manage().window().maximize();
        sleep(3000);
        webDriver.manage().window().fullscreen();
        sleep(3000);
        webDriver.manage().window().setSize(new Dimension(600,1000));
    }

    private static void test06() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //判断元素是否可以被点击
        WebDriverWait wait = new WebDriverWait(webDriver,3000);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div > p:nth-child(8)")));
        wait.until(ExpectedConditions.titleIs("百度一下，你就知道"));
    }

    private static void test05() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();
        if (url.equals("https://www.baidu.com/") && title.equals("百度一下，你就知道")) {
            System.out.println("url:" + url +",title:" + title);
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
    }

    private static void test04() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        String botton = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
        if (botton.equals("百度一下")) {
            System.out.println("测试通过");
        }else {
            System.out.println(botton);
            System.out.println("测试不通过");
        }
    }

    private static void test03() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.xpath("//a[text()=\"新闻\"]")).click();
    }

    private static void test02() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //找到百度输入框 输入Java107
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("Java107");
        //点击百度一下按钮
        webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        //清空百度搜索输入框中的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();

    }

    private static void test01() throws InterruptedException {
        int flg = 0;
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //找到百度搜索输入框
        //WebElement webElement = webDriver.findElement(By.cssSelector(".s_ipt"));
        WebElement webElement = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //输入软件测试
        webElement.sendKeys("软件测试");
        //点击百度一下 找到百度一下按钮 点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //校验
        //找到搜索结果
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0; i < elements.size(); i++) {
            //System.out.println(elements.get(i).getText());
            //如果返回的结果有软件测试 证明测试通过,否则不通过
            if (elements.get(i).getText().equals("测试")) {
                flg = 1;
                System.out.println("测试通过");
                break;
            }
        }
        if (flg == 0) {
            System.out.println("测试不通过");
        }
    }
}
